/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.capiq.client.view;

import java.util.ArrayList;
import java.util.List;

import com.excilys.capiq.client.service.QuestionServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AddQuestionWidget extends Composite {

	interface AddQuestionBinder extends UiBinder<Widget, AddQuestionWidget> {}

	private static AddQuestionBinder	uiBinder			= GWT.create(AddQuestionBinder.class);

	private final QuestionServiceAsync	questionService;

	@UiField
	FlowPanel							answersPanel;

	@UiField
	TextArea							question;

	@UiField
	TextArea							explanation;

	@UiField
	Button								add;

	@UiField
	InlineLabel							saving;

	private List<TextBox>				answerTextBoxes		= new ArrayList<TextBox>();
	private List<RadioButton>			answerRadioButtons	= new ArrayList<RadioButton>();

	public AddQuestionWidget(QuestionServiceAsync questionService) {
		initWidget(uiBinder.createAndBindUi(this));

		this.questionService = questionService;

		initAnswerPanel();
	}

	private void initAnswerPanel() {
		addAnswerBox();
		answerRadioButtons.get(0).setValue(true);
		addAnswerBox();
	}

	@UiHandler("addAnswer")
	public void addAnswer(ClickEvent click) {
		addAnswerBox();
	}

	private void addAnswerBox() {
		final FlowPanel answerPanel = new FlowPanel();
		final TextBox answerTextbox = new TextBox();

		answerTextbox.setWidth("500px");
		final RadioButton answerRadioButton = new RadioButton("answerButtons");

		answerRadioButtons.add(answerRadioButton);
		answerTextBoxes.add(answerTextbox);

		Anchor deleteAnchor = new Anchor("Supprimer");

		deleteAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				answerTextBoxes.remove(answerTextbox);
				answerRadioButtons.remove(answerRadioButton);
				answerPanel.removeFromParent();
			}
		});

		answerPanel.add(answerRadioButton);
		answerPanel.add(answerTextbox);
		answerPanel.add(deleteAnchor);

		answersPanel.add(answerPanel);
	}

	@UiHandler("add")
	public void addQuestion(ClickEvent click) {

		String questionValue = question.getText();
		String explanationValue = explanation.getText();
		List<String> answerValues = new ArrayList<String>();

		int i = 0;
		int goodAnswer = 0;
		for (TextBox answerTextBoxe : answerTextBoxes) {
			answerValues.add(answerTextBoxe.getText());
			if (answerRadioButtons.get(i).getValue()) {
				goodAnswer = i;
			}

			i++;
		}

		add.setVisible(false);
		saving.setVisible(true);

		questionService.createQuestion(questionValue, explanationValue, answerValues, goodAnswer, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Erreur " + caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				add.setVisible(true);
				question.setText("");
				answersPanel.clear();
				answerTextBoxes.clear();
				answerRadioButtons.clear();
				explanation.setText("");
				initAnswerPanel();
				add.setVisible(true);
				saving.setVisible(false);
			}
		});

	}
}
